#!/bin/sh
# postrm script for rabbitmq
#
# see: dh_installdeb(1)

set -e

# summary of how this script can be called:
#        * <postrm> `remove'
#        * <postrm> `purge'
#        * <old-postrm> `upgrade' <new-version>
#        * <new-postrm> `failed-upgrade' <old-version>
#        * <new-postrm> `abort-install'
#        * <new-postrm> `abort-install' <old-version>
#        * <new-postrm> `abort-upgrade' <old-version>
#        * <disappearer's-postrm> `disappear' <overwriter>
#          <overwriter-version>
# for details, see https://www.debian.org/doc/debian-policy/ or
# the debian-policy package

remove_plugin_traces() {
    # Remove traces of plugins
    rm -rf /var/lib/rabbitmq/plugins-scratch
}

remove_autocomplete_script() {
    if [ -f /etc/profile.d/rabbitmqctl-autocomplete.sh ]; then
        rm -rf /etc/profile.d/rabbitmqctl-autocomplete.sh
        rm -rf /usr/share/zsh/vendor-functions/_enable_rabbitmqctl_completion
    fi
}

case "$1" in
    purge)
        rm -f /etc/default/rabbitmq
        if [ -d /var/lib/rabbitmq ]; then
                rm -r /var/lib/rabbitmq
        fi
        if [ -d /var/log/rabbitmq ]; then
                rm -r /var/log/rabbitmq
        fi
        if [ -d /etc/rabbitmq ]; then
                rm -r /etc/rabbitmq
        fi
        remove_autocomplete_script
        remove_plugin_traces
        if getent passwd rabbitmq >/dev/null; then
                # Stop epmd if run by the rabbitmq user
                pkill -u rabbitmq epmd || :
        fi
    ;;

    remove)
        remove_autocomplete_script
        remove_plugin_traces
    ;;
    upgrade)
        remove_plugin_traces
    ;;

    failed-upgrade|abort-install|abort-upgrade|disappear)
    ;;

    *)
        echo "postrm called with unknown argument \`$1'" >&2
        exit 1
    ;;
esac

# dh_installdeb will replace this with shell code automatically
# generated by other debhelper scripts.

#DEBHELPER#

exit 0
